package com.tarena.lbs.controller;

import com.tarena.lbs.entity.Coupon;
import com.tarena.lbs.entity.CouponActivity;
import com.tarena.lbs.entity.UserCoupons;
import com.tarena.lbs.service.CouponActivityService;
import com.tarena.lbs.service.CouponService;
import com.tarena.lbs.service.UserCouponsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/coupon")
@Tag(name = "优惠券接口")
@Api(value = "优惠券接口", tags = "优惠券接口")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @Autowired
    private CouponActivityService couponActivityService;

    @Autowired
    private UserCouponsService userCouponsService;

    @PostMapping("/add")
    @Operation(summary = "添加优惠券")
    @ApiOperation(value = "添加优惠券", notes = "创建一个新的优惠券")
    public boolean addCoupon(
            @ApiParam(value = "优惠券实体对象", required = true, name = "coupon")
            @RequestBody Coupon coupon
    ) {
        return couponService.addCoupon(coupon);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取优惠券详情")
    @ApiOperation(value = "根据ID获取优惠券详情", notes = "通过优惠券ID获取对应的优惠券详细信息")
    public Coupon getCoupon(
            @ApiParam(value = "优惠券ID", required = true, name = "id", example = "1")
            @PathVariable Integer id
    ) {
        return couponService.getCouponById(id);
    }

    @GetMapping("/activity/{activityId}")
    @Operation(summary = "根据活动ID获取优惠券列表")
    @ApiOperation(value = "根据活动ID获取优惠券列表", notes = "通过活动ID获取该活动下的所有优惠券列表")
    public List<Coupon> getCouponsByActivity(
            @ApiParam(value = "活动ID", required = true, name = "activityId", example = "1")
            @PathVariable Integer activityId
    ) {
        return couponService.listCouponsByActivityIdWithoutActivityIdField(activityId);
    }

    @GetMapping("/user/{userId}")
    @Operation(summary = "根据用户ID获取用户优惠券列表")
    @ApiOperation(value = "根据用户ID获取用户优惠券列表", notes = "通过用户ID获取该用户持有的所有优惠券列表")
    public List<UserCoupons> getUserCoupons(
            @ApiParam(value = "用户ID", required = true, name = "userId", example = "1")
            @PathVariable Integer userId
    ) {
        return userCouponsService.listUserCouponsByUserId(userId);
    }

    @PostMapping("/receive")
    @Operation(summary = "用户领取优惠券")
    @ApiOperation(value = "用户领取优惠券", notes = "用户根据优惠券ID和活动ID领取优惠券")
    public boolean receiveCoupon(
            @ApiParam(value = "用户ID", required = true, name = "userId", example = "1")
            @RequestParam Integer userId,
            @ApiParam(value = "优惠券ID", required = true, name = "couponId", example = "1")
            @RequestParam Integer couponId,
            @ApiParam(value = "活动ID", required = false, name = "activityId", example = "1")
            @RequestParam(required = false) Integer activityId
    ) {
        return userCouponsService.receiveCoupon(userId, couponId, activityId);
    }

    @PostMapping("/use")
    @Operation(summary = "用户使用优惠券")
    @ApiOperation(value = "用户使用优惠券", notes = "用户根据优惠券码和订单编号使用优惠券")
    public boolean useCoupon(
            @ApiParam(value = "优惠券码", required = true, name = "couponCode", example = "ABC123")
            @RequestParam String couponCode,
            @ApiParam(value = "订单编号", required = true, name = "orderNo", example = "ORDER123")
            @RequestParam String orderNo
    ) {
        return userCouponsService.useCoupon(couponCode, orderNo);
    }

    @GetMapping("/activities/{userGroup}")
    @Operation(summary = "根据用户分组获取可参与的活动列表/")
    @ApiOperation(value = "根据用户分组获取可参与的活动列表", notes = "根据用户分组信息获取该用户可参与的所有优惠券活动列表")
    public List<CouponActivity> getActivitiesByUserGroup(
            @ApiParam(value = "用户分组", required = true, name = "userGroup", example = "VIP")
            @PathVariable String userGroup
    ) {
        return couponActivityService.listActivitiesByUserGroup(userGroup);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除优惠券")
    @ApiOperation(value = "删除优惠券", notes = "根据优惠券ID删除对应的优惠券")
    public boolean deleteCoupon(
            @ApiParam(value = "优惠券ID", required = true, name = "id", example = "1")
            @PathVariable Integer id
    ) {
        return couponService.deleteCoupon(id);
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除优惠券")
    @ApiOperation(value = "批量删除优惠券", notes = "根据优惠券ID列表批量删除对应的优惠券")
    public boolean batchDeleteCoupons(
            @ApiParam(value = "优惠券ID列表", required = true, name = "ids", example = "1,2,3")
            @RequestParam List<Integer> ids
    ) {
        return couponService.batchDeleteCoupons(ids);
    }
}